<template>
    <div>
        <el-row class="login">
            <el-col :span="6" :offset="9">
                <div class="grid-content">
                    <el-input placeholder="用户名" v-model="uid">
                        <div slot="prepend" class="fa fa-user icon"></div>
                    </el-input>
                </div>
                <div class="grid-content">
                    <el-input type="password" placeholder="密码" v-model="pwd">
                        <div slot="prepend" class="fa fa-lock icon"></div>
                    </el-input>
                </div>
                <div class="msg">{{msg}}</div>
                <el-button style="width: 100%;" type="info" @click="login()">登&nbsp&nbsp&nbsp&nbsp&nbsp录</el-button>
            </el-col>
        </el-row>
    </div>
</template>

<style lang="less">
    .login {
        margin: 100px 0;
    }
    .grid-content {
        margin: 20px 0;
    }
    .icon {
        width: 20px;
        text-align: center;
    }
    .msg {
        font-size: 12px;
        color: red;
        text-align: center;
        margin: 10px auto;
    }
</style>

<script>
    import {API_ROOT, Domain} from '../config'
    import * as AuthService from '../utils/authService'
    import cookie from 'react-cookie'
//    import {$rootscope} from '../utils/$rootscope'
    import _ from 'lodash'
    import {crossDomainStorage} from '../utils/localStorageTools/src/crossDomainStorage'

    import Vue from 'vue'

    export default {
        name: 'app',
        components: {
        },
        ready() {
            console.log('hoho');
        },
        created() {
            console.log(this.$data, this.$root)
            console.log('created!???')
        },
        data() {
            return {
                uid: '',
                pwd: '',
                msg: '',
            }
        },
        sockets: {
            connect: function() {
                console.log('login socket connected!',this.$socket.id)
            },
            message: function(dt) {
                this.message(dt);
            },
            notify: function(dt) {
                this.$notify(dt);
            }
        },
        methods: {
            login() {
                localStorage.removeItem('hc-crm')
                localStorage.removeItem('hc-sales')
                localStorage.removeItem('userinfo')
                const that = this;
                var instance = this.$http.create(
                        {
                            headers: {
                                'jwt-access-token': '131452c',
                                'socket-id': that.$socket.id
                            }
                        }
                );

                instance.post(API_ROOT + '/manage/login',
                        {
                            uid: that.uid,
                            pwd: that.pwd
                        }).then(function(dt) {
                            if(dt.data.status.code == 0 && dt.data.data) {
                                var sys = dt.data.data;
                                var logined = false;
                                var serviceError = true;
                                that.$root.sys = sys;
                                for(var i =0; i< sys.length; i++) {
                                    console.log(sys[i].name, JSON.stringify(sys[i]),'登录成功!');
                                    localStorage.setItem(sys[i].name, JSON.stringify(sys[i]));
                                    console.log('this is authorize - - - - - -- ', sys[i]['authorize']);
                                    if(sys[i].status == 0) {
                                        logined = true;
                                        serviceError = false;
//                                        var reg = /((http|ftp|https):\/\/)/g;
//                                        var reged = sys[i].url.replace(reg);
                                        if(sys[i].name == 'hc-sales') {
                                            var cdstorage = crossDomainStorage({
                                                origin: "http://localhost:5584",
                                                path: "/localstorage/crossd_iframe.html"
                                            });
                                            cdstorage.setItem('pond-jwt-token', sys[i]['authorize'])
                                        }

//                                        AuthService.saveCookie(sys[i].name, JSON.stringify(sys[i]));

//                                        var domain = sys[i].url.replace(/((http|ftp|https):\/\/)/g, '')
//                                        var path = '/'
//                                        console.log(domain,path,sys[i]['authorize'],' this is domain && path')

//                                        cookie.save('name', 'bbbb', {
//                                            domain: 'localhost:1920',
//                                            path: '/b'
//                                        });
//
//                                        cookie.save('x-pond-sessionid', 'pondid',
//                                                {
//                                                    domain: domain,
//                                                    path: path
//                                                }
//                                        )
                                    }else if(sys[i].status == 1) {
                                        serviceError = false
                                    }
                                }
                                if(logined) {
                                    that.$notify({
                                        type: 'success',
                                        message: '登录成功!'
                                    })
                                    var userinfo = {
                                        name: that.uid,
                                        pwd: that.pwd
                                    }
                                    localStorage.setItem("userinfo", JSON.stringify(userinfo));
                                    that.$router.push('/choosesys');
                                } else{
                                    if(serviceError) {
                                        that.$message({
                                            type: 'error',
                                            message: '服务器连接出错'
                                        })
                                    }else {
                                        that.$message({
                                            type: 'error',
                                            message: '登录失败,用户名或密码错误'
                                        })
                                    }
                                }
                            }else {
                                that.$message({
                                    type: 'error',
                                    message: '登录失败!'
                                })
                            }
                            console.log('dt',dt);
                })
            },
            login1() {
                const that = this;
                var instance = this.$http.create(
                        {
                            withCredentials: true,
                            headers: {
                                'jwt-access-token': '131452c',
                                'socket-id': that.$socket.id
                            }

                        }
                );
                instance.post(API_ROOT + '/manage/login1',
                        {
                            uid: that.uid,
                            pwd: that.pwd
                        }).then(function(dt) {
                            console.log(dt, 'this is dt');

                })
            }
        }
    }
</script>

